﻿#!/usr/bin/env
# _*_ coding:utf-8 _*_

from tkinter import *
from tkinter import messagebox
# pip install pillow
from PIL import Image,ImageTk
import requests
import re
import os

folder = '签名图片'
if not os.path.exists(folder):
	os.mkdir(folder)
os.chdir(folder)

# 模拟浏览器请求
def download():
	startUrl = 'http://www.uustv.com/'
	#获取用户输入的姓名
	name = entry.get()
	#去空格
	name = name.strip()
	
	if name == '':
		messagebox.showinfo('提示:','请输入名字')
	else:
		data = {
			'word':name,
			'sizes':'60',
			'fonts':'jfcs.ttf',
			'fontcolor':'#000000'
		}
		result = requests.post(startUrl,data=data)
		result.encoding = 'utf-8'
		#获取网页的源代码
		html = result.text

		p = '<div class="tu">﻿<img src="(.*?)"/></div>'
		imagePath = re.findall(p,html)[0]
		#print(startUrl+imagePath)
		#获取完整图片路径
		imgUrl = startUrl+imagePath
		#获取图片内容
		response = requests.get(imgUrl).content
		
		f = open('{}.gif'.format(name),'wb')
		f.write(response)
		f.close()
		
		
		bm = ImageTk.PhotoImage(file='{}.gif'.format(name))
		label2 = Label(root,image = bm)
		label2.bm = bm
		label2.grid(row=2, columnspan=2)
	
#创建窗口
root = Tk()
#标题
root.title('签名设计')
#窗口大小
root.geometry('600x300')
#窗口位置
root.geometry('+500+200')
#标签的一个控件
label = Label(root,text='签名',font=('华文行楷',20),fg='red')
# pack  grid  place  布局
label.grid()
#输入框
entry = Entry(root,font=('微软雅黑',20))
entry.grid(row=0,column=1)
#点击按钮
button = Button(root,text = '设计签名',font=('微软雅黑',20),command = download)
button.grid(row=1,column=0)

#消息循环 显示窗口
root.mainloop()




